/**
 * All rights Reserved, Designed By Android_Robot
 *
 * @Title: DeviceUtil.java
 * @Package com.swift.utils
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: HYM
 * @date: 2017-5-22 下午2:55:23
 * @version V1.0
 */
package com.swift.utils;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * @ClassName: DeviceUtil
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-5-22 下午2:55:23
 *
 */
public class DeviceUtil {

    public static DeviceUtil instance;
    private Context context;

    public static DeviceUtil getInstance() {
        if (instance == null) {
            synchronized (DeviceUtil.class) {
                if (instance == null) {
                    instance = new DeviceUtil();
                }
            }
        }
        return instance;
    }

    public void init(Context context) {
        this.context = context;
    }

    // 网络是否连接（包括Wifi和移动网络）
    public boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = cm.getActiveNetworkInfo();
        if (network != null) {
            return network.isAvailable();
        }
        return false;
    }

    // Wifi是否可用
    public boolean isWifiEnable() {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        return wifiManager.isWifiEnabled();
    }

    // Gps是否可用
    public boolean isGpsEnable() {
        LocationManager locationManager = ((LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE));
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    public float getBiLi() {
        return context.getResources().getDisplayMetrics().density;
    }

//	public int getIntBiLi() {
//		return (int)context.getResources().getDisplayMetrics().density;
//	}

    /**
     * 计算出来的位置，y方向就在anchorView的上面和下面对齐显示，x方向就是与屏幕右边对齐显示
     * 如果anchorView的位置有变化，就可以适当自己额外加入偏移来修正
     *
     * @param anchorView
     *            呼出window的view
     * @param contentView
     *            window的内容布局
     * @return window显示的左上角的xOff, yOff坐标
     */
    public int[] calculatePopWindowPos(final View anchorView,
                                       final View contentView) {
        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 获取锚点View在屏幕上的左上角坐标位置
        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorWidth = anchorView.getWidth();
        final int anchorHeight = anchorView.getHeight();
        // 获取屏幕的高宽
        final int screenHeight = getScreenHeight();
        final int screenWidth = getScreenWidth();

        final int windowHeight = culListView((ListView) contentView);

        // 判断需要向上弹出还是向下弹出显示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1]
                - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - anchorWidth;
            windowPos[1] = anchorLoc[1] - windowHeight;
        } else {
            windowPos[0] = screenWidth - anchorWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        return windowPos;
    }

    /**
     * 获取屏幕高度(px)
     */
    public int getScreenHeight() {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 获取屏幕宽度(px)
     */
    public int getScreenWidth() {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public int culListView(ListView listView) {
//		int[] widthheight = new int[]{0,0};

        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return 0;
        }
        int totalHeight = 0;
//		int totalWidth = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
//			if (listItem.getMeasuredWidth() > totalWidth) {
//				totalWidth = listItem.getMeasuredWidth();
//			}
        }
//		widthheight[1] = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//		widthheight[0] = totalWidth;
        return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    }

    /**
     * 获取输入法管理
     * */
    public InputMethodManager getImm() {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        return imm;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public int[] getLocationOnScreen(View view) {

        int[] location = new int[2];
        view.getLocationOnScreen(location);
        location[0] = location[0] + view.getWidth() / 2;//获取当前位置的横坐标
//        location[1] = location[1] + view.getHeight()/2;//获取当前位置的纵坐标
        return location;
    }
}
